package com.test.servlet;

//import com.google.gson.Gson;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;


public class HelloServlet implements Servlet {

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

        //当前时间对象
        Date time =  new Date();

        //时间转字符串
        String New_time = time.toString();
        System.out.println(New_time);

        //时间格式化
        SimpleDateFormat ft = new SimpleDateFormat("y-M-d H:m:s");
        System.out.println("当前时间为: " + ft.format(time));

        //服务请求解析
        HttpServletRequest  Request= (HttpServletRequest)servletRequest;
        String method = Request.getMethod();
        System.out.println(method);

        //接收参数
        String name = servletRequest.getParameter("name");

        //数组
        ArrayList<String> sites = new ArrayList<String>();
        ArrayList<String> objectName =new ArrayList<String>();  // 初始化
        //Hash
        Map<String, String> map = new HashMap<String, String>();


        //新建学生类
        Student student = new Student();

        ArrayList<String> books = new ArrayList<String>();
        books.add("数学");
        books.add("语文");
        books.add("英语");
        books.add("物理");
        books.add("化学");
        books.add("生物");
        student.books = books;

        HashMap<String, String> booksMap = new HashMap<String, String>();
        booksMap.put("1", "数学");
        booksMap.put("2", "语文");
        booksMap.put("3", "英语");
        booksMap.put("4", "物理");
        booksMap.put("5", "化学");
        booksMap.put("6", "生物");
        student.booksMap = booksMap;

//        Gson gson = new Gson();
//        try {
//            // 调用GSON jar工具包封装好的toJson方法，可直接生成JSON字符串
//
////            String stu = gson.toJson(student);
////            System.out.println(stu);
//        } catch (Exception e) {
//            System.out.println("错误信息：" + e.getMessage());
//        }




        //        String json = gson.toJson(nt);

        // 输出到界面
        System.out.println("ggggggggggggggggggxxxxxxxxxx");
        servletResponse.setContentType("text/plain");
        servletResponse.setCharacterEncoding("gb2312");
        servletResponse.setContentType("text/html;charset=utf-8");
        PrintWriter out = new PrintWriter(servletResponse.getOutputStream());
        out.print("sssssssssssssssssssxxxxxxxxxxx");
        out.flush();


    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
